// Declare the TaskManager Task namespace.
TaskManager.Task = {}

// Show all the values for the task.
TaskManager.Task.show = function(e) {
	$("#task-id").val(e.view.params.id);
	$("#task-back").attr("href", "#sprint?id=" + $("#sprint-id").val());
	
	var task = TaskManager.DataAccess.getTask(e.view.params.id);
	
	TaskManager.DataAccess.getDevelopers(function(developers) {
		var assignedto = $("#task-assignedto");
		assignedto.empty();
		
		$.each(developers, function(idx, developer) {
			var option = $("<option/>")
				.attr("value", developer.DeveloperId)
				.text(developer.FirstName + " " + developer.LastName);
			
			if (developer.DeveloperId == task.AssignedToId) {
				option.attr("selected", "selected");
			}
			assignedto.append(option);
		});
		try {
			assignedto.kendoDropDownList();
		} catch (err) {
		}
	});
	
	$("#task-type").val(task.TypeOfTask);
	$("#task-description").val(task.Description);
	$("#task-estimated-hours").val(task.EstimatedHours);
	$("#task-completed").val(Date.fromRaw(task.CompletedDate).toShortDate());
}

TaskManager.Task.save = function() {
	var id = $("#task-id").val();
	var task = TaskManager.DataAccess.getTask(id);
	
	task.Description = $("#task-description").val();
	task.EstimatedHours = $("#task-estimated-hours").val();
	var completedDate = new Date($("#task-completed").val());
	if (completedDate != "Invalid Date") {
		task.CompletedDate = completedDate.toRaw();
	}
	TaskManager.DataAccess.saveTask(task, function(response) {
		if (response.result.length == 0) {
			TaskManager.Task.results(true, "Green", "Save Complete");

			// After 2 seconds, hide the message.
			setTimeout('$("#task-save-results").fadeOut("slow", function() { TaskManager.Task.results(false); });', 2000);
		}
	});
}

TaskManager.Task.results = function(show, color, message) {
	var $results = $("#task-save-results");
	
	if (show) {
		$results.css("display", "").css("position", "").css("color", color).text(message);
	} else {
		$results.css("display", "none").css("position", "absolute").text("");
	}
}